from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect, render


class AuthMiddleware(MiddlewareMixin):
    def process_request(self, request):
        # 0 排除那些不需要登录就能访问的页面
        # request.path_info 获取当前用户请求的URL
        # if request.path_info == "/login/":
        if request.path_info in ["/login/", "/image/code/"]:
            return

        # 1 读取当前用户登录的访问session信息，能读取到，说明已经登录，继续往后执行
        info_dict = request.session.get("info")
        if info_dict:
            return

        # 2 如果没有登录信息,重新回到登录页面
        return redirect('/login/')
